/**
 * 
 */
package com.zito.pojo;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;


import com.downlove.cache.CacheManager;
import com.downlove.database.DBException;
import com.downlove.database.POJO;
import com.downlove.database.QueryHelper;
import com.downlove.database.SequenceManager;
import com.zito.bean.Albumphoto;

/**
 * @author YunHai.Jiang
 * 
 */
public class AlbumPOJO extends POJO {
	
	private Object[] valueObj;
	//private String __this_table_name;

	/**
	 * 
	 */
	private static final long serialVersionUID = 8331442234739752036L;

	public long Save(String sessionid) {
		this.setId(SequenceManager.nextID(this.getClass().getSimpleName()));
		boolean flag = true;
		try {
			QueryHelper.setAutoCommit();
			QueryHelper.commitUpdate(_InsertSQL(this), this.valueObj);
			QueryHelper.commitUpdate((new Albumphoto()).updateObject(),
					this.getId(), sessionid);
			QueryHelper.commit();
		} catch (Exception e) {
			flag = false;
			try {
				QueryHelper.rollback();
			} catch (SQLException e1) {
				throw new DBException(e1);
			}
			throw new DBException(e);
		} finally {
			try {
				QueryHelper.closeTransConnection();
			} catch (SQLException e2) {
				throw new DBException(e2);
			}
		}
		if (this.IsObjectCachedByID())
			CacheManager.evict(CacheRegion(), OBJ_COUNT_CACHE_KEY);
		return flag ? this.getId() : -1;
	}
	
	public long Update(String sessionid) {
		boolean flag = true;
		try {
			QueryHelper.setAutoCommit();
			QueryHelper.commitUpdate(_updateSQL("title","content","photo","id"), this.valueObj);
			QueryHelper.commitUpdate((new Albumphoto()).updateObject(), this
					.getId(), sessionid);
			QueryHelper.commit();
		} catch (Exception e) {
			flag = false;
			try {
				QueryHelper.rollback();
			} catch (SQLException e1) {
				throw new DBException(e1);
			}
			throw new DBException(e);
		} finally {
			try {
				QueryHelper.closeTransConnection();
			} catch (SQLException e2) {
				throw new DBException(e2);
			}
		}
		if (this.IsObjectCachedByID())
			CacheManager.evict(CacheRegion(), OBJ_COUNT_CACHE_KEY);
		return flag ? this.getId() : -1;
	}
	
	private String _updateSQL(String... args) {
		Map<String, Object> pojo_bean = this.ListInsertableFields();
		StringBuilder sql = new StringBuilder("UPDATE ");
		sql.append(TableName());
		sql.append(" SET ");
		valueObj = new Object[args.length];
		for (int i = 0; i < args.length-1; i++) {
			if (i > 0)
				sql.append(',');
			sql.append(args[i] + "=?");
			valueObj[i] = pojo_bean.get(args[i]);
		}
		valueObj[args.length-1] = pojo_bean.get(args[args.length-1]);
		//sql.append(" WHERE " + args[args.length-1] +"="+pojo_bean.get(args[args.length-1]));
		sql.append(" WHERE " + args[args.length-1] +"=?");
		return sql.toString();
	}

	private String _InsertSQL(AlbumPOJO obj) {
		Map<String, Object> pojo_bean = obj.ListInsertableFields();
		String[] fields = pojo_bean.keySet().toArray(
				new String[pojo_bean.size()]);
		StringBuilder sql = new StringBuilder("INSERT INTO ");
		sql.append(obj.TableName());
		sql.append('(');
		valueObj = new Object[fields.length];
		for (int i = 0; i < fields.length; i++) {
			if (i > 0)
				sql.append(',');
			sql.append(fields[i]);
		}
		sql.append(") VALUES(");
		for (int i = 0; i < fields.length; i++) {
			if (i > 0)
				sql.append(',');
			sql.append('?');
			valueObj[i] = pojo_bean.get(fields[i]);
		}
		sql.append(')');
		return sql.toString();
	}
	

	
	public List<Long> idListFilter(String filter, int page, int size) {
		String sql = "SELECT id FROM " + TableName() + " WHERE " + filter
				+ " ORDER BY posttime DESC";
		return QueryHelper.queryList(sql, page, size);
	}
}
